package com.fishtel.photoslider;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize();
    }
    
    private void initialize() {

    	fillPhotosList();
    	PhotoSliderAdapter adapter = new PhotoSliderAdapter(_photoViews);
    	
        ViewPager _photoSlider = new ViewPager(this);
        _photoSlider.setAdapter(adapter);
        _photoSlider.setCurrentItem(1);     
        setContentView(_photoSlider);
   }
    
    private void fillPhotosList() {
    	_photoViews = new ArrayList<View>();
    	List<Bitmap> photos = createPhotoList();
    	
    	for(Bitmap photo : photos) {
    		_photoViews.add(createPhotoView(photo));	
    	}
    }
    
    private List<Bitmap> createPhotoList() {
    	String imagePath = "";
    	List<Bitmap> photos = new ArrayList<Bitmap>();
    	for(int i=1; i<6; i++) {
    		imagePath = String.valueOf(i) + ".jpg";
            try {
                InputStream ins = getAssets().open(imagePath);
                photos.add(BitmapFactory.decodeStream(ins));
          } catch (final IOException e) {
                e.printStackTrace();
          }    		
    	}
    	
    	return photos;
    }
    
    private View createPhotoView(Bitmap photo) {
    	LayoutInflater ltInflater = getLayoutInflater();
        View view = ltInflater.inflate(R.layout.photo_slider_item, null, false);
        ZoomableImageView photoView = (ZoomableImageView)view.findViewById(R.id.photo_slider_item);
    	photoView.setImageBitmap(photo);
    	
    	return view;
    }
    
    private List<View> _photoViews;
    private ViewPager _photoSlider;
}
